package com.upyun;

import java.io.File;
import java.util.Map;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.upyun.block.api.listener.CompleteListener;
import com.upyun.block.api.listener.ProgressListener;
import com.upyun.block.api.main.UploaderManager;
import com.upyun.block.api.utils.UpYunUtils;



public class TestActivity extends Activity {

    // 空间名
    String bucket = "hzjaikan";
    // 表单密钥
    String formApiSecret = "/qs/xPw/PJvgmGeCebjaHz4TJX8=";
    // 本地文件路径
    private String localFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"crophead1.jpg";
    // 保存到又拍云的路径
    String savePath = "avatar/avatar_crophead1.jpg";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        new UploadTask().execute();
    }

    public class UploadTask extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {
            Log.i("xxxx","localFilePath:"+localFilePath);
            File localFile = new File(localFilePath);
            try {
				/*
				 * 设置进度条回掉函数
				 * 
				 * 注意：由于在计算发送的字节数中包含了图片以外的其他信息，最终上传的大小总是大于图片实际大小，
				 * 为了解决这个问题，代码会判断如果实际传送的大小大于图片
				 * ，就将实际传送的大小设置成'fileSize-1000'（最小为0）
				 */
                ProgressListener progressListener = new ProgressListener() {
                    @Override
                    public void transferred(long transferedBytes, long totalBytes) {
                        // do something...
                        System.out.println("trans:" + transferedBytes + "; total:" + totalBytes);
                    }
                };

                CompleteListener completeListener = new CompleteListener() {
                    @Override
                    public void result(boolean isComplete, String result, String error) {
                        // do something...
                        System.out.println("isComplete:"+isComplete+";result:"+result+";error:"+error);
                    }
                };

                UploaderManager uploaderManager = UploaderManager.getInstance(bucket);
                uploaderManager.setConnectTimeout(60);
                uploaderManager.setResponseTimeout(60);
                Map<String, Object> paramsMap = uploaderManager.fetchFileInfoDictionaryWith(localFile, savePath);
                //还可以加上其他的额外处理参数...
                //paramsMap.put("return_url", "http://httpbin.org/get");
                // signature & policy 建议从服务端获取
                String policyForInitial = UpYunUtils.getPolicy(paramsMap);
                String signatureForInitial = UpYunUtils.getSignature(paramsMap, formApiSecret);
                uploaderManager.upload(policyForInitial, signatureForInitial, localFile, progressListener, completeListener);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return "result";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result != null) {
                Log.i("xxxx","localFilePath:result:"+result);
                Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_LONG).show();
            }
        }
    }
}